package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.MyOfferEntity2;
import com.swgk.core.util.UserOperating;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.common.base.BaseViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.SharedPreferenceUtil;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 杨畔
 * @date: 2021/9/22 15:07
 * @Description: 普通货源-我的报价子页面
 */
public class NormalMyOfferPageViewModel extends BaseViewModel {
    public MutableLiveData<ListResponse<List<MyOfferEntity2>>> ldRecord = new MutableLiveData<>();
    private int page = 1;

    public NormalMyOfferPageViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void getMyOfferOrder(Context context, int status, boolean isRefresh) {
        Map<String, Object> map = new HashMap<>();
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        map.put("page", page);
        map.put("limit", 15);

            map.put("searchType", 3);

        //货源模式 0 普通模式 1 竞价模式
        map.put("pattern", 0);
        if (status != 0) {
            map.put("status", status);
        }
        //
        int type = UserOperating.getInstance().getUserIdentity();
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        String userNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, "");
        if (type == 1) {//企业
            map.put("companyNo", companyNo);
            map.put("offerType", 1);
        } else if (type == 2) {//团队
            map.put("teamNo", companyNo);
            map.put("offerType", 2);
        } else {//个人
            map.put("userNo", userNo);
            map.put("offerType", 3);
        }
        DialogHelper.showProgressDialog(context, "数据加载中...");
        NewAPIManager.getInstance().getLogisticsAPI().getMyOfferList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<MyOfferEntity2>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<MyOfferEntity2>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    ldRecord.postValue(data.getData());
                    if (data.getData().haveMoreData()) {
                        page = data.getData().nextPage();
                    }
                } else {
                    ToastUtil.show(context, data.getMsg());
                }
            }

            @Override
            public void onFail() {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
